package com.zenithmind.news.controller;

import com.zenithmind.common.result.Result;
import com.zenithmind.news.mapper.NewsMapper.NewsStatistics;
import com.zenithmind.news.mapper.NewsMapper.TagStatistics;
import com.zenithmind.news.mapper.NewsMapper.NewsTrend;
import com.zenithmind.news.service.NewsStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 新闻统计控制器 - 遵循单一职责原则
 * 专门负责新闻的统计分析功能
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "新闻统计", description = "新闻统计相关接口")
@RestController
@RequestMapping("/api/zenithMind/v1/news/statistics")
@RequiredArgsConstructor
public class NewsStatisticsController {

    private final NewsStatisticsService newsStatisticsService;

    @Operation(summary = "批量更新新闻状态", description = "批量更新新闻状态")
    @PostMapping("/batch/status")
    public Result<Boolean> batchUpdateStatus(
            @Parameter(description = "新闻ID列表") @RequestParam List<String> newsIds,
            @Parameter(description = "新闻状态") @RequestParam Integer status) {
        Boolean result = newsStatisticsService.batchUpdateStatus(newsIds, status);
        return Result.success(result);
    }

    @Operation(summary = "获取新闻统计信息", description = "获取新闻统计信息")
    @GetMapping("/overview")
    public Result<NewsStatistics> getNewsStatistics(
            @Parameter(description = "分类ID") @RequestParam(required = false) String categoryId,
            @Parameter(description = "开始时间") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        NewsStatistics statistics = newsStatisticsService.getNewsStatistics(categoryId, startTime, endTime);
        return Result.success(statistics);
    }

    @Operation(summary = "获取热门标签", description = "获取热门标签列表")
    @GetMapping("/tags/hot")
    public Result<List<TagStatistics>> getHotTags(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<TagStatistics> tags = newsStatisticsService.getHotTags(limit);
        return Result.success(tags);
    }

    @Operation(summary = "获取新闻趋势", description = "获取新闻趋势数据")
    @GetMapping("/trend")
    public Result<List<NewsTrend>> getNewsTrend(
            @Parameter(description = "天数") @RequestParam(defaultValue = "7") Integer days) {
        List<NewsTrend> trend = newsStatisticsService.getNewsTrend(days);
        return Result.success(trend);
    }

    @Operation(summary = "计算新闻热度", description = "计算指定新闻的热度评分")
    @PostMapping("/{id}/hot-score")
    public Result<Double> calculateHotScore(@Parameter(description = "新闻ID") @PathVariable String id) {
        Double hotScore = newsStatisticsService.calculateHotScore(id);
        return Result.success(hotScore);
    }
}
